package com.rk.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rk.common.result.Result;
import com.rk.common.utils.MD5;
import com.rk.hosp.model.hosp.HospitalSet;
import com.rk.hosp.service.HospitalSetService;
import com.rk.hosp.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Random;

/**
 * @author :Rk.
 * @date : 2022/10/26
 */
@Slf4j
@RestController
@Api(tags = "医院设置接口")
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;


    @ApiOperation("获取所有医院设置信息")
    @GetMapping("/findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }


    @ApiOperation("医院设置信息删除")
    @DeleteMapping("/{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation(("分页查询医院设置信息"))
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        System.out.println(current);
        System.out.println(limit);
        System.out.println(hospitalSetQueryVo);
        Page<HospitalSet> page=new Page<>(current,limit);
        LambdaQueryWrapper<HospitalSet> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(hospitalSetQueryVo!=null){
            String hoscode = hospitalSetQueryVo.getHoscode();
            String hosname = hospitalSetQueryVo.getHosname();
            //医院名称不为空  带条件

            if(hosname!=null&&hosname!=""){
                lambdaQueryWrapper.like(HospitalSet::getHosname,hosname);
            }
            //医院编号
            if(hoscode!=null&&hoscode!="")
            {
                lambdaQueryWrapper.eq(HospitalSet::getHoscode,hoscode);
            }
        }

        hospitalSetService.page(page,lambdaQueryWrapper);

        return Result.ok(page);

    }


    @ApiOperation("添加医院设置信息")
    @PostMapping("/saveHospitalSet")
    public Result addHospitalSet(@RequestBody(required = true) HospitalSet  hospitalSet){
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //保存
        boolean flag = hospitalSetService.save(hospitalSet);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }



    @ApiOperation("根据id获取医院设置信息")
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }


    @ApiOperation("修改医院设置信息")
    @PostMapping("/updateHospitalSet")
    public Result updateHospSet(@RequestBody(required = true) HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    @ApiOperation("批量删除医院设置信息")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        boolean flag = hospitalSetService.removeByIds(ids);
        return flag?Result.ok():Result.fail();

    }


    @ApiOperation("医院锁定和解锁")
    @PostMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }


    @ApiOperation("发送签名key")
    @PutMapping("/sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }

}
